package com.banciyuan.bcywebview.biz.groupdetail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.write.WriteGridAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by squall on 2015/8/24.
 */
public class AddGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PhotoModel> slist = new ArrayList<>();
    private Map<String, Bitmap> map = new HashMap<>();
    private AddGroupActivity.HeaderHolder headerHolder;
    private LayoutInflater inflater;
    private Context mContext;


    public AddGroupAdapter(List<PhotoModel> slist, Context mContext, AddGroupActivity.HeaderHolder headerHolder) {
        this.slist = slist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.headerHolder = headerHolder;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return headerHolder;
        } else {
            return new ViewImage(inflater.inflate(R.layout.add_group_grid_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 1:
                ViewImage viewImage = (ViewImage) holder;
                renderImage(viewImage, position - 1);
                break;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        if (slist.size() > 0 && slist.size() < 9) {
            return slist.size() + 2;
        } else {
            return slist.size() + 1;
        }
    }

    private class ViewImage extends RecyclerView.ViewHolder {
        ImageView img_content;
        ImageView img_cancel;
        TextView progress;

        public ViewImage(View convertView) {
            super(convertView);
            img_content = (ImageView) convertView.findViewById(R.id.item_group_content);
            img_cancel = (ImageView) convertView.findViewById(R.id.item_group_cancel);
            progress = (TextView) convertView.findViewById(R.id.progress);
        }
    }

    private void renderImage(final ViewImage viewImage, final int position) {
        if (position < slist.size() && !slist.isEmpty()) {
            final String path = slist.get(position).getOriginalPath();
            if (map.get(path) == null) {
                new AsyncTask<Void, Bitmap, Bitmap>() {
                    @Override
                    protected Bitmap doInBackground(Void... params) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inSampleSize = 4;
                        return BitmapFactory.decodeFile(path, options);
                    }

                    @Override
                    protected void onPostExecute(Bitmap bitmap) {
                        if (bitmap != null) {
                            if (position < slist.size())
                                map.put(path, bitmap);
                            viewImage.img_content.setImageBitmap(bitmap);
                            setProgress(slist.get(position), viewImage);
                        }

                    }
                }.execute();
            } else {
                viewImage.img_content.setImageBitmap(map.get(slist.get(position).getOriginalPath()));
                setProgress(slist.get(position), viewImage);
            }
            viewImage.img_cancel.setVisibility(View.VISIBLE);
            viewImage.img_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                    alphaAnimation.setDuration(200);
                    ((View) (view.getParent())).startAnimation(alphaAnimation);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (!slist.isEmpty() && position < slist.size()) {
                                slist.remove(position);
                            }
                            AddGroupAdapter.this.notifyDataSetChanged();
                        }
                    }, alphaAnimation.getDuration());
                }
            });
        } else {
            viewImage.img_content.setImageDrawable(mContext.getResources().getDrawable(R.drawable.publish_add));
            viewImage.img_cancel.setVisibility(View.GONE);
            viewImage.progress.setVisibility(View.GONE);
            viewImage.img_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((AddGroupActivity) mContext).startImagePic();
                }
            });
        }
    }

    private void setProgress(PhotoModel p, ViewImage imageHolder) {
        if (!p.isUpload()) {
            imageHolder.progress.setVisibility(View.VISIBLE);
            imageHolder.progress.setText(String.valueOf((int) (p.getProgress() * 100)) + "%");
        } else {
            imageHolder.progress.setVisibility(View.GONE);
        }
    }
}
